package com.cncsi.web;

import com.cncsi.dao.UserRepository;
import com.cncsi.entity.Orders;
import com.cncsi.entity.User;
import com.cncsi.entity.zTree;
import com.cncsi.service.OrderService;
import com.cncsi.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;
import java.util.Map;

/**
 * Created by liuyf90 on 2018/9/9.
 */
@Controller
@RequestMapping("/order")
public class OrderController {

    @Autowired
    private UserService userService;
    @Autowired
    private OrderService orderService;

    @RequestMapping(value = {"/", "/{id}"}, method = RequestMethod.GET)
    public ModelAndView init(@PathVariable(value = "id", required = false) Long id, Model model) {
        return new ModelAndView("order/main", "model",  null);
    }

    @RequestMapping(value = "/toOrderIndex", method = RequestMethod.GET)
    public String toOrgIndex(Map<String, Object> map) {
        List<User> userList = userService.getUserList();
        map.put("userList", userList);
        List<Orders> ordersList = orderService.findOrderForAll();
        map.put("ordersList", ordersList);
        return "order/order_index";
    }

    @RequestMapping(value = "/toOrderUpdate", method = RequestMethod.GET)
    public String toOrderUpdate(Map<String, Object> map, Long ordersId) {
        List<User> userList = userService.getUserList();
        map.put("userList", userList);
        Orders orders = orderService.findOrderForOne(ordersId);
        map.put("orders", orders);
        return "order/order_update";
    }

    @ResponseBody
    @RequestMapping(value = "/saveOrder", method = RequestMethod.POST)
    public String saveOrder(Orders orders) {
        String result = orderService.saveOrder(orders);
        return result;
    }

    @ResponseBody
    @RequestMapping(value = "/deleteOrder", method = RequestMethod.POST)
    public String deleteOrder(Long ordersId) {
        String result = orderService.deleteOrder(ordersId);
        return result;
    }
}
